package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.UserMapper;
import com.jdz.pojo.User;
import com.jdz.service.EmailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service("EmailService")
public class EmailServiceImpl implements EmailService {


    @Autowired
    UserMapper userMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private SimpleMailMessage simpleMailMessage;

    /**
     * @方法名: sendMailSimple
     * @参数名：@param subject  邮件主题
     * @参数名：@param content 邮件内容
     * @参数名：@param to     收件人Email地址
     * @描述语: 发送邮件
     */
    @Override
    public ServerResponse sendMailSimple(HttpSession session,String stuid){


        try {

            User user = userMapper.checkStuId(stuid);

            String yanzhen = String.valueOf((Math.random()*9+1)*100000);

            if(StringUtils.isBlank(user.getStuemail())){
                return ServerResponse.createByErrorMessage("没有绑定邮箱信息");
            }

            //用于接收邮件的邮箱
            simpleMailMessage.setTo(user.getStuemail());
            //邮件的主题
            simpleMailMessage.setSubject("金点子项目");
            //邮件的正文，第二个boolean类型的参数代表html格式
            simpleMailMessage.setText("【金点子】验证码："+yanzhen+"，在五分钟内有效，如非本人操作请忽视本邮件。");

            session.setAttribute("yanzhen",yanzhen+"|"+System.currentTimeMillis()+"|"+stuid);
            //发送
            javaMailSender.send(simpleMailMessage);

            return ServerResponse.createBySuccessMessage("发送邮件成功");
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("发送邮件出错，可能是没有绑定邮箱");
        }
    }


    public ServerResponse setEmaill(HttpSession session,String yanzhenFrom,String password){
        String yanzhenString = (String)session.getAttribute("yanzhen");
        if(StringUtils.isBlank(yanzhenString)){
            return  ServerResponse.createByErrorMessage("没有验证码");
        }
        String[] yanzhenList = yanzhenString.split("|");
        if(yanzhenList.length!=3){
            return  ServerResponse.createByErrorMessage("验证码错误");
        }

        String yanzhen = yanzhenList[0];
        if(StringUtils.isBlank(yanzhen)){
            return  ServerResponse.createByErrorMessage("没有验证码");
        }
        long dateFrom  = Long.parseLong(yanzhenList[1]);

        long  date = System.currentTimeMillis();

        if(date-dateFrom>=30000){
            return ServerResponse.createByErrorMessage("超过五分钟，验证码过期");
        }
        if(yanzhen.equals(yanzhenFrom)){
            return  ServerResponse.createByErrorMessage("验证码错误");
        }else {
            int r = userMapper.updatePasswordByUsername(yanzhenList[2],password);
            if(r<=0){
                return ServerResponse.createByErrorMessage("修改密码错误");
            }
            session.setAttribute("yanzhen",null);
            return ServerResponse.createBySuccessMessage("修改成功");
        }
    }

}
